package py.com.fms.persistence.services;

import java.sql.SQLException;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

public class StatusTypeHandlerCallback implements TypeHandlerCallback {
	
	public static final String YES = "S";
	public static final String NO = "N";

	public Object getResult(ResultGetter getter) throws SQLException {
		// TODO Auto-generated method stub
		return stringToBoolean(getter.getString());
	}

	public void setParameter(ParameterSetter setter, Object object)
			throws SQLException {
		// TODO Auto-generated method stub
		setter.setString(booleanToString((Boolean) object));

	}

	public Object valueOf(String s) {
		// TODO Auto-generated method stub
		return stringToBoolean(s);
	}
	
	private Boolean stringToBoolean(String s) {
		if (YES.equalsIgnoreCase(s)) {
			return Boolean.TRUE;
		} else if (NO.equalsIgnoreCase(s)) {
			return Boolean.FALSE;
		} else {
			throw new IllegalArgumentException("Could not convert " + s
					+ " to a boolean value. "
					+ "Valid arguments are 'S' and 'N'.");
		}
	}

	private String booleanToString(Boolean b) {
		if (b == null) {
			throw new IllegalArgumentException(
					"Could not convert null to a boolean value. "
							+ "Valid arguments are 'true' and 'false'.");
		} else if (b.booleanValue()) {
			return YES;
		} else {
			return NO;
		}
	}
	

}
